/*
* 时间格式化过滤器
*/

// 取剩余秒
const pluralize = (time, label) => {
  return time + label + '前'
}

// 相对时间过滤器，传入时间，返回距离今天有多久
export const timeAgo = time => {
  time = time instanceof Date ? time : new Date(time)
  const between = Date.now() / 1000 - (Number(time) / 1000)
  if (between < 3600) {
    if (Object.is(~~(between / 60), 0)) return '刚刚'
    return pluralize(~~(between / 60), ' 分钟')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' 小时')
  } else {
    return pluralize(~~(between / 86400), ' 天')
  }
}

// 转换为本地时间格式
export const toLocalString = date => {
  return date ? new Date(date).toLocaleString() : date
}

// YMDHMS时间转换过滤器
export const toYMD = date => {
  if (!date) return date
  date = new Date(date)
  return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours() > 11 ? '下午' : '上午'}`
}

// 转化为YYYY-MM-DD hh:mm:ss
export const toFullTimeString = date => {
  if (!date) return date
  date = new Date(date)
  let MM = date.getMonth() + 1,
	  DD = date.getDate(),
	  hh = date.getHours(),
	  mm = date.getMinutes(),
	  ss = date.getSeconds()
  return `${date.getFullYear()}-${MM>9?MM:'0'+MM}-${DD>9?DD:'0'+DD} ${hh>9?hh:'0'+hh}:${mm>9?mm:'0'+mm}:${ss>9?ss:'0'+ss}`
}

// 转化为YYYY/MM/DD
export const toDateFormat = date => {
  if (!date) return date
  date = new Date(date)
  let MM = date.getMonth() + 1,
  	  DD = date.getDate(),
  	  hh = date.getHours(),
  	  mm = date.getMinutes(),
  	  ss = date.getSeconds()
  return `${date.getFullYear()}/${MM>9?MM:'0'+MM}/${DD>9?DD:'0'+DD}`
}